package com.atguigu.order.service.impl;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.order.bean.Order;
import com.atguigu.order.feign.ProductFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired//一定导入spring-cloud-starter-loadbalancer
    LoadBalancerClient loadBalancerClient;
    @Autowired
    ProductFeignClient productFeignClient;
    @Override
    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    public Order createOrder(Long productId, Long userId) {
//      Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        //总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("张三");
        order.setAddress("尚硅谷");
        //远程查询商品列表
        order.setProductsList(Arrays.asList(product));
        return order;
    }
    //执行兜底回调
    public Order createOrderFallback(Long productId, Long userId, BlockException  e){
         Order order = new Order();
         order.setId(0L);
         order.setTotalAmount(new BigDecimal("0"));
         order.setUserId(userId);
         order.setNickName("未知用户");
         order.setAddress("异常信息"+e.getClass());
         return order;
    }
    //没有负载均衡
    private Product getProductFromRemote(Long productId) {
        //获取到商品服务所在的所有机器ip+post
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        //远程 url
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求:{}", url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
    //完成负载均衡发送请求
    private Product getProductFromRemoteWithLoadBalance(Long productId) {
        //获取到商品服务所在的所有机器ip+post
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        //远程 url
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求:{}", url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
    //进阶版，基于注解完成负载均衡发送请求
    private Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId) {
        //远程 url
       String url = "http://service-product/product/" + productId;
       log.info("远程请求:{}", url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
